/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mahumLogs.utils;

import com.mahumLogs.backedbeans.Domain;
import com.mahumLogs.backedbeans.Log;
import com.mahumLogs.backedbeans.LogID;

/**
 *
 * @author Kalast
 */
public class LogParser {

    public static Log parse(String line, int numLine){
        Log l = null;
        String[] elements = line.replaceAll("\\s+", " ").split(" ");
        int requestTime = -1;
        int requestSize = -1;
        int requestStatus = -1;
        String ip = "";
        String url = "";
        String mimetype = "";
        boolean errors = false;
       
        LogID id = new LogID();
        id.id = numLine;

        try {
            id.time = Long.valueOf(elements[0].replace(".", ""));
        } catch (Exception ex) {
            //System.err.println("Erreur sur le timestamp");
            errors = true;
        }

        try {
            requestTime = Integer.valueOf(elements[1]);
        } catch (Exception ex) {
            //System.err.println("Erreur sur le requestTime");
            errors = true;
        }

        try {
            requestSize = Integer.valueOf(elements[4]);
        } catch (Exception ex) {
            //System.err.println("Erreur sur le requestSize");
            errors = true;
        }

        try {
            ip = elements[2];
        } catch (Exception ex) {
            //System.err.println("Erreur sur l'IP");
            errors = true;
        }

        try {
            requestStatus = Integer.valueOf(elements[3].split("/")[1]);
        } catch (Exception ex) {
            //System.err.println("Erreur sur le requestStatus");
            errors = true;
        }

        try {
            url = elements[6];
        } catch (Exception ex) {
            //System.err.println("Erreur sur l'URL");
            errors = true;
        }
        
        try {
            url = elements[6];
        } catch (Exception ex) {
            //System.err.println("Erreur sur l'URL");
            errors = true;
        }
        
        try {
            mimetype = elements[9];
        } catch (Exception ex) {
            //System.err.println("Erreur sur le MIMETYPE");
            errors = true;
        }

        if (!errors) {
            try{
                String domaine = Domain.getDomain(url);
                if(domaine != null){
                    l = new Log(
                        id,
                         domaine,
                        requestTime,
                        requestSize,
                        ip,
                        url,
                        requestStatus,
                        mimetype
                    );
                }
            } catch(Exception ex){
                
            }
        } else {
            //System.err.println("Une erreur est survenue lors du parsing de la ligne '" + line + "'");
        }

        return l;
    }
}
